package com.shengsheng.expschedule.service.ucenter.controller.admin;


import com.shengsheng.expschedule.base.result.R;
import com.shengsheng.expschedule.service.ucenter.entity.Vo.RoleInfoVo;
import com.shengsheng.expschedule.service.ucenter.service.RoleService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 会员角色表 前端控制器
 * </p>
 *
 * @author yuan
 * @since 2021-11-04
 */
@Api(tags = "角色管理")
@RestController
@RequestMapping("/admin/ucenter/role")
@RefreshScope
public class RoleController {

    @Autowired
    private RoleService roleService;

    @ApiOperation("添加角色")
    @PostMapping("/save")
    public R save(@ApiParam(value = "角色对象", required = true)
                  @RequestBody RoleInfoVo roleVo) {
        if (roleService.saveByRoleVo(roleVo)) return R.ok().message("添加成功");
        return R.error().message("添加失败");
    }

    @ApiOperation("删除角色")
    @DeleteMapping("/remove/{id}")
    public R remove(@ApiParam(value = "角色 Id", required = true)
                    @PathVariable String id) {
        if (roleService.removeRoleById(id)) return R.ok().message("删除成功");
        return R.error().message("删除失败");
    }

    @ApiOperation("给指定角色分配权限")
    @PostMapping("/assignRights/{id}/{rightIds}")
    public R assignRightsByRoleId(@ApiParam(value = "角色 Id", required = true)
                                  @PathVariable String id,
                                  @ApiParam(value = "权限 Id 数据", required = true)
                                  @PathVariable String rightIds) {
        if (roleService.assignRightsByRoleId(id, rightIds)) return R.ok().message("分配成功");
        return R.error().message("分配失败");
    }

    @ApiOperation("查询所有角色信息")
    @GetMapping("/getRoleList")
    public R getRoleList() {
        return R.ok()
                .data("roleList", roleService.getRoleList())
                .message("获取角色列表信息成功");
    }

    @ApiOperation("查询指定的角色信息")
    @GetMapping("/getRole/{id}")
    public R getRole(@ApiParam(value = "角色 id", required = true)
                     @PathVariable String id) {
        return R.ok()
                .data("role", roleService.getById(id))
                .message("获取数据成功");
    }

    @ApiOperation("查询指定角色的权限信息")
    @GetMapping("/getRightList/{id}")
    public R getRightList(@ApiParam(value = "角色 id", required = true)
                     @PathVariable String id) {
        return R.ok()
                .data("rightList", roleService.getRightListById(id))
                .message("获取数据成功");
    }

    @ApiOperation("获取所有角色名称")
    @GetMapping("/getRoleNameList")
    public R getRoleNameList() {
        return R.ok()
                .data("roleNameList", roleService.getRoleNameList())
                .message("获取数据成功");
    }

}

